home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 426-450 / disk_441 / dme / src / subs.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  8KB  |  424 lines

  1.  
  2. /*
  3.  *  SUBS.C
  4.  *
  5.  *    (C)Copyright 1987 by Matthew Dillon, All Rights Reserved
  6.  *
  7.  *  Subroutines.
  8.  */
  9.  
  10. #include "defs.h"
  11.  
  12. Prototype void makemygadget (struct Gadget *);
  13. Prototype int firstns (char *);
  14. Prototype int lastns (char *);
  15. Prototype int wordlen (char *);
  16. Prototype int getpathto (char *, char *);
  17. Prototype void *allocb (int);
  18. Prototype void *allocl (int);
  19. Prototype void bmovl (void *, void *, long);
  20. Prototype int detab (char *, char *, int);
  21. Prototype int xefgets (FILE *, char *, int);
  22. Prototype int ncstrcmp (unsigned char *, unsigned char *);
  23. Prototype struct _ED *finded (char *, int);
  24. Prototype void mountrequest(int);
  25. Prototype int DeadKeyConvert(struct IntuiMessage *, UBYTE *, int, struct KeyMap *);
  26. Prototype FONT * GetFont(char *, short);
  27. Prototype char *GetDEnv(char *);
  28. Prototype void SetDEnv(char *, char *);
  29.  
  30. Prototype extern __stkargs int arpreq (char *, char *, char *, WIN *);
  31.  
  32. Prototype extern void *clrmem(void *, size_t);
  33.  
  34.  
  35. typedef struct FileInfoBlock FIB;
  36. typedef struct Process         PROC;
  37.  
  38. extern void *OpenFont();
  39. extern void *OpenDiskFont();
  40. extern void *OpenLibrary();
  41.  
  42. /*
  43.  *  Create DME's text icon.
  44.  */
  45.  
  46. void
  47. makemygadget(gad)
  48. struct Gadget *gad;
  49. {
  50.     static unsigned long ga[] = {
  51.     0xFFFFFFFF,    /* 32 pixels across */
  52.     0x80FDCBFD,
  53.     0xFFFDDFFD,
  54.     0x80000001,
  55.     0x80DFDDDF,
  56.     0x80000001,
  57.     0xBC0EF00B,
  58.     0x80000001,
  59.     0xBFC00CDD,
  60.     0x80000001,
  61.     0xA00DF00F,
  62.     0x80000001,
  63.     0x80000001,
  64.  
  65.     0x80000001,
  66.     0x80FDCBFD,
  67.     0xFFFDDFFD,
  68.     0x80000001,
  69.     0x80DFDDDF,
  70.     0x80000001,
  71.     0xBC0EF00B,
  72.     0x80000001,
  73.     0xBFC00CDD,
  74.     0x80000001,
  75.     0xA00DF00F,
  76.     0x80000001,
  77.     0xFFFFFFFF
  78.     };
  79.     static struct Image image = {
  80.     0, 0, 20, 16, 2, (unsigned short *)ga, 3, 0, NULL
  81.     };
  82.     clrmem(gad, sizeof(struct Gadget));
  83.     gad->Width = 20;
  84.     gad->Height = 17;
  85.     gad->Flags    = GADGIMAGE|GADGHCOMP;
  86.     gad->GadgetType   = BOOLGADGET;
  87.     gad->Activation = RELVERIFY|GADGIMMEDIATE;
  88.     gad->GadgetRender = (APTR)ℑ
  89. }
  90.  
  91. /*
  92.  * return index of first non space.  Returns 0 if no spaces found.
  93.  */
  94.  
  95. firstns(str)
  96. char *str;
  97. {
  98.     short i;
  99.  
  100.     for (i = 0; str[i] && str[i] == ' '; ++i);
  101.     if (str[i] == 0)
  102.     i = 0;
  103.     return((int)i);
  104. }
  105.  
  106. /*
  107.  *  Return index of last non-space, 0 if no spaces.
  108.  */
  109.  
  110. lastns(str)
  111. char *str;
  112. {
  113.     short i;
  114.  
  115.     for (i = strlen(str) - 1; i > 0 && str[i] == ' '; --i);
  116.     if (i < 0)
  117.     i = 0;
  118.     return((int)i);
  119. }
  120.  
  121. /*
  122.  *  Return length of word under cursor
  123.  */
  124.  
  125. wordlen(str)
  126. char *str;
  127. {
  128.     short i;
  129.  
  130.     for (i = 0; *str && *str != ' '; ++i, ++str);
  131.     return((int)i);
  132. }
  133.  
  134. /*
  135.  *  Find the path from some root device to a specific filename (src), and
  136.  *  stick the result in (dest).  If unable to backtrace along directories,
  137.  *  simply copy (src) into (dest)
  138.  *
  139.  *  Returns (1) if able to backtrace, (0) if not.
  140.  */
  141.  
  142. getpathto(src, dest)
  143. char *src, *dest;
  144. {
  145.     BPTR flock, pflock;
  146.     short len, total;
  147.     FIB *fib = (FIB *)malloc(sizeof(FIB));
  148.     char c;
  149.  
  150.     dest[0] = 0;
  151.     total = 1;
  152.     flock = Lock(src, ACCESS_READ);
  153.     if (flock == NULL) {                           /* missing file?    */
  154.     for (len = strlen(src); len >= 0; --len) {
  155.         if (src[len] == '/') {
  156.         --len;
  157.         break;
  158.         }
  159.         if (src[len] == ':')
  160.         break;
  161.     }
  162.     if (c = src[len + 1]) {
  163.         strcpy(dest, src+len+2);
  164.         total = strlen(dest)+1;
  165.     }
  166.     src[len + 1] = 0;
  167.     flock = Lock(src, ACCESS_READ);
  168.     src[len + 1] = c;
  169.     }
  170.     if (flock) {
  171.     do {
  172.         pflock = ParentDir(flock);
  173.         if (Examine(flock, fib) == 0)
  174.         fib->fib_FileName[0] = 0;
  175.         if (fib->fib_FileName[0] == 0)
  176.         strcpy(fib->fib_FileName, "ram");
  177.         len = strlen(fib->fib_FileName);
  178.         movmem(dest, dest + len + 1, total);
  179.         dest[len] = (pflock) ? '/' : ':';
  180.         movmem(fib->fib_FileName, dest, len);
  181.         total += len + 1;
  182.         UnLock(flock);
  183.         flock = pflock;
  184.     } while(pflock);
  185.     len = strlen(dest) - 1;
  186.     if (dest[len] == '/')
  187.         dest[len] = 0;
  188.     return(1);
  189.     }
  190.     strcpy(dest, src);
  191.     return(0);
  192. }
  193.  
  194. /*
  195.  *  Allocation routines and other shortcuts
  196.  */
  197.  
  198. void *
  199. allocb(bytes)
  200. {
  201.     return(AllocMem(bytes, MEMF_CLEAR|MEMF_PUBLIC));
  202. }
  203.  
  204. void *
  205. allocl(lwords)
  206. {
  207.     return(AllocMem(lwords<<2, MEMF_CLEAR|MEMF_PUBLIC));
  208. }
  209.  
  210. void
  211. bmovl(s,d,n)
  212. void *s, *d;
  213. long n;
  214. {
  215.     movmem(s, d, n << 2);
  216. }
  217.  
  218. /*
  219.  *  Remove tabs in a buffer
  220.  */
  221.  
  222. detab(ibuf, obuf, maxlen)
  223. char *ibuf, *obuf;
  224. {
  225.     short i, j;
  226.  
  227.     maxlen -= 2;
  228.     for (i = j = 0; ibuf[i] && j < maxlen; ++i) {
  229.     if (ibuf[i] == 9) {
  230.         do {
  231.         obuf[j++] = ' ';
  232.         } while ((j & 7) && j < maxlen);
  233.     } else {
  234.         obuf[j++] = ibuf[i];
  235.     }
  236.     }
  237.     if (j && obuf[j-1] == '\n')
  238.     --j;
  239.     while (j && obuf[j-1] == ' ')
  240.     --j;
  241.     obuf[j] = 0;
  242.     return((int)j);
  243. }
  244.  
  245. xefgets(fi, buf, max)
  246. FILE *fi;
  247. char *buf;
  248. int max;
  249. {
  250.     char ebuf[256];
  251.  
  252.     if (fgets(ebuf, max, fi))
  253.     return(detab(ebuf, buf, max));
  254.     return(-1);
  255. }
  256.  
  257. ncstrcmp(s1, s2)
  258. ubyte *s1, *s2;
  259. {
  260.     ubyte c1, c2;
  261.  
  262.     for (;;) {
  263.     c1 = *s1;
  264.     c2 = *s2;
  265.     if (c1 >= 'A' && c1 <= 'Z') c1 |= 0x20;
  266.     if (c2 >= 'A' && c2 <= 'Z') c2 |= 0x20;
  267.     if (c1 != c2)
  268.         break;
  269.     if ((c1|c2) == 0)
  270.         return(0);
  271.     ++s1;
  272.     ++s2;
  273.     }
  274.     if (c1 < c2)
  275.     return(-1);
  276.     if (c1 > c2)
  277.     return(1);
  278. }
  279.  
  280. ED *
  281. finded(str, doff)
  282. char *str;
  283. {
  284.     ED *ed;
  285.  
  286.     for (ed = (ED *)DBase.mlh_Head; ed->Node.mln_Succ; ed = (ED *)ed->Node.mln_Succ) {
  287.     if (strlen(ed->Name) >= doff && ncstrcmp(str, ed->Name+doff) == 0)
  288.         return(ed);
  289.     }
  290.     return(NULL);
  291. }
  292.  
  293. void
  294. mountrequest(bool)
  295. int bool;
  296. {
  297.     static APTR original_pr_WindowPtr = NULL;
  298.     register PROC *proc;
  299.  
  300.     proc = (PROC *)FindTask(0);
  301.     if (!bool && proc->pr_WindowPtr != (APTR)-1) {
  302.     original_pr_WindowPtr = proc->pr_WindowPtr;
  303.     proc->pr_WindowPtr = (APTR)-1;
  304.     }
  305.     if (bool && proc->pr_WindowPtr == (APTR)-1)
  306.     proc->pr_WindowPtr = original_pr_WindowPtr;
  307. }
  308.  
  309. char *
  310. GetDEnv(ename)
  311. char *ename;
  312. {
  313.     long envLock = Lock("env:", SHARED_LOCK);
  314.     char *str = NULL;
  315.  
  316.     if (envLock) {
  317.     long oldLock = CurrentDir(envLock);
  318.     FILE *fi = fopen(ename, "r");
  319.     long siz;
  320.     if (fi) {
  321.         fseek(fi, 0L, 2);
  322.         siz = ftell(fi);
  323.         fseek(fi, 0L, 0);
  324.         if (siz > 0 && (str = malloc(siz + 1))) {
  325.         fread(str, siz, 1, fi);
  326.         str[siz] = 0;
  327.         }
  328.         fclose(fi);
  329.     }
  330.     UnLock(CurrentDir(oldLock));
  331.     }
  332.     return(str);
  333. }
  334.  
  335. void
  336. SetDEnv(ename, econt)
  337. char *ename;
  338. char *econt;
  339. {
  340.     long envLock = Lock("env:", SHARED_LOCK);
  341.  
  342.     if (envLock) {
  343.     long oldLock = CurrentDir(envLock);
  344.     FILE *fi = fopen(ename, "w");
  345.  
  346.     if (fi) {
  347.         fwrite(econt, strlen(econt), 1, fi);
  348.         fclose(fi);
  349.     }
  350.     UnLock(CurrentDir(oldLock));
  351.     }
  352. }
  353.  
  354.  
  355. /*
  356.  *  GETFONT()
  357.  *
  358.  *  This function properly searches resident and disk fonts for the
  359.  *  font.
  360.  */
  361.  
  362. struct Library *DiskfontBase;
  363.  
  364. FONT *
  365. GetFont(name, size)
  366. char *name;
  367. short size;
  368. {
  369.     FONT *font1;
  370.     TA Ta;
  371.     short libwasopen = (DiskfontBase != (void *)NULL);
  372.  
  373.     Ta.ta_Name    = (UBYTE *)name;
  374.     Ta.ta_YSize = size;
  375.     Ta.ta_Style = 0;
  376.     Ta.ta_Flags = 0;
  377.  
  378.     font1 = OpenFont(&Ta);
  379.     if (font1 == NULL || font1->tf_YSize != Ta.ta_YSize) {
  380.     FONT *font2;
  381.  
  382.     if (libwasopen || (DiskfontBase = OpenLibrary("diskfont.library", 0))) {
  383.         if (font2 = OpenDiskFont(&Ta)) {
  384.         if (font1)
  385.             CloseFont(font1);
  386.         font1 = font2;
  387.         }
  388.         if (libwasopen == 0)
  389.         CloseLibrary(DiskfontBase);
  390.     }
  391.     }
  392.     return(font1);
  393. }
  394.  
  395. /*
  396.  *  DEAD.C
  397.  */
  398.  
  399. int
  400. DeadKeyConvert(msg,buf,bufsize,keymap)
  401. struct IntuiMessage *msg;
  402. UBYTE *buf;
  403. int bufsize;
  404. struct KeyMap *keymap;
  405. {
  406.     static struct InputEvent ievent = { NULL, IECLASS_RAWKEY };
  407.     if (msg->Class != RAWKEY)
  408.     return(-2);
  409.     ievent.ie_Code = msg->Code;
  410.     ievent.ie_Qualifier = msg->Qualifier;
  411.     ievent.ie_position.ie_addr = *((APTR *)msg->IAddress);
  412.     return(RawKeyConvert(&ievent,(char *)buf,bufsize,keymap));
  413. }
  414.  
  415. void *
  416. clrmem(ptr, bytes)
  417. void *ptr;
  418. long bytes;
  419. {
  420.     setmem(ptr, bytes, 0);
  421.     return(ptr);
  422. }
  423.  
  424.